<?php
plugin('admin-top-edit-static-content');
if($_GET['cleanDB'] == true) {
	$THEME_URL = THEME_URL;
	echo <<<EOT
	<iframe src="http://{$_SERVER['HTTP_HOST']}{$THEME_URL}" height="1" width="1"></iframe>
	<h1>Cleaning up the database....</h1>
	<p>Done! You will redirected back in 2 seconds.</p>
	<script type="text/javascript">
	<!--
	setTimeout('window.location="http://{$_SERVER['HTTP_HOST']}{$THEME_URL}admin.php?page=edit-static-content"', 2000);
	//-->
	</script>	
EOT;
exit();
}
if(file_exists(BASE_URL.'includes/admin/editors/fckeditor/fckeditor.php')) {
	include(BASE_URL.'includes/admin/editors/fckeditor/fckeditor.php');
	$rte = true;
}
else {
	$rte = false;
}
$id = sanitize_sql_string($_GET['id']);
if($_POST['save']) {
	foreach($_POST as $key => $value) {
		$_POST[$key] = sanitize_sql_string($value);
	}
	$sql = "UPDATE ".DBTABLEPREFIX."content_template SET content = '".$_POST['static-content']."' WHERE id = '".$_POST['id']."'";
	$run = $admin->db_query($sql)or die($output .= $admin->message('error', 'MySQL Error', $admin->db->error()));
	if(!$run) {
		$output .= $admin->message('error', 'Unknown Error', 'See above.');
	}
	else {
		$output .= $admin->message('success', 'Woohoo! Successfully updated the static content!', 'Congrats! The chanages are applied accross all pages!');
	}
}
elseif($_GET['id']) {
	$id = sanitize_sql_string($_GET['id']);
	$staticContent = $theme->arrayStaticContent();
	foreach($staticContent as $key => $value) {
		$sid = $value['id'];
		$name = $value['name'];
		if($sid == $id) {
			$title = $name;
			$content = $value['content'];
			$textOnly = $value['textOnly'];
		}
	}
	if($textOnly == "1")
		$textOnly = 'yes';
	if($textOnly != "yes" || $rte == false) {
			$FCKeditor_content = new FCKeditor('static-content') ;
			$FCKeditor_content->BasePath = THEME_URL.'includes/admin/editors/fckeditor/';
			$FCKeditor_content->Value = stripslashes($content);
			$FCKeditor_content->Width = '100%' ;
			$FCKeditor_content->Height = '350' ;
			$FCKeditor_content_html = $FCKeditor_content->CreateHtml();
	}
	else {
		$FCKeditor_content_html = '<textarea name="static-content" cols="50" rows="20" style="width:95%;">'.stripslashes(stripslashes(stripslashes($content))).'</textarea>';
	}
		$form = '
							<form action="?page=edit-static-content" method="post">
								<fieldset>
									<h2>Edit Static Content "'.$title.'" </h2>
									<div class="form-row">
										<label class="content" for="content">Static Content</label>
										<span class="content">
											'.$FCKeditor_content_html.'
										</span>
									</div>
									<div class="form-row form-row-last">
										<label for="save">Save</label>
										<span><input type="hidden" name="id" value="'.$id.'" /><input type="submit" name="save" value="Update Static Content" class="submit" /></span>
									</div>
								</fieldset>
							</form>
		';
}
else {
}
	$THEME_URL = THEME_URL;
	$staticContent = $theme->arrayStaticContent();
	$table .= <<<EOT
	<h2>Static Content</h2>
	<p class="pageDesc">Static content is content that will be the same accross all of the pages.
	This is useful for something like a list of links to resources that you recommend.</p>

EOT;
	$table .= $admin->message('warning', 'Are these static content areas correct?', 'Do some of these static content areas not exist anymore? Are content areas that should be here but aren\'t here? Click <a href="admin.php?page=edit-static-content&cleanDB=true">here</a> to fix the problem.');
	$table .= <<<EOT
	<table style="margin-top:0;" cellspacing='0' border='0' id='static-content' cellpadding='0'>
		<thead>
			<tr>
				<th>Name</th><th>Template</th><th class='td-actions'>Actions</th>
			</tr>
		</thead>
		<tbody>

EOT;
	foreach($staticContent as $key => $value) {
		$id = $value['id'];
		$name = $value['name'];
		$table .= <<<EOT
			<tr>
				<td class='static-name'><a style='display:block;' title='Edit the {$name}' class='helplink' href='?page=edit-static-content&id={$id}'>{$name}</a></td>
				<td class='current-theme'>{$admin->page_info['settings']['Theme']['value']}</td>
				<td class='td-actions'><a href='?page=edit-static-content&id={$id}' title='Edit the {$name}' class='helplink'><img src='{$THEME_URL}includes/admin/images/icons/edit-16x16.png' alt='Edit' /></a></td>
			</tr>

EOT;
	}
	$table .= "\t\t</tbody>\n\t</table>";	

$admin->page_info['content']['page_title'] = "Edit Static Content";
$admin->page_info['content']['page_content']	= $output.$table.$form;
plugin('admin-bottom-edit-static-content');
//Display page
include(BASE_URL.'includes/admin/admin.php');
?>